import blue
import trinity
import spaceObject
import destiny
import uthread
import nodemanager
import timecurves
import base
import util
import turret
import random
import turret
from string import split
entityExplosionsS = ['res:/Emitter/tracerexplosion/NPCDeathS1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathS3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathS4.blue']
entityExplosionsM = ['res:/Emitter/tracerexplosion/NPCDeathM1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathM3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathM4.blue']
entityExplosionsL = ['res:/Emitter/tracerexplosion/NPCDeathL1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathL3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathL4.blue']

class TurretCollection():

    def __init__(self):
        self.turrets = []
        self.activeTurret = -1



    def Initialize(self, ship, type):
        locatorCount = ship.GetTurretLocatorCount()
        if (locatorCount == 0):
            return 
        for x in xrange(1, ((int((locatorCount / 2)) + (locatorCount % 2)) + 1)):
            newTurret = turret.NewTurret()
            eveTurretPair = newTurret.Initialize(type, (('locator_turret_' + str(x)) + 'a'), (('locator_turret_' + str(x)) + 'b'))
            ship.turrets.append(eveTurretPair)
            self.turrets.append(newTurret)




    def Release(self):
        for each in self.turrets:
            each.Release()




    def SetTarget(self, shipID, targetID):
        for each in self.turrets:
            each.SetTarget(shipID, targetID)




    def SetAmmoColor(self, ammoTypeID):
        for each in self.turrets:
            each.SetAmmoColor(ammoTypeID)




    def StartShooting(self, ammoGFXid = None):
        for each in self.turrets:
            each.StartShooting(ammoGFXid)




    def Rest(self):
        for each in self.turrets:
            each.Rest()




    def ActivateFluff(self, fluffname, active):
        return 



    def StopShooting(self):
        for each in self.turrets:
            each.StopShooting()





class TurretCollection_Sc2(TurretCollection):

    def Initialize(self, ship, type):
        locatorCount = ship.GetTurretLocatorCount()
        if (locatorCount == 0):
            return 
        for x in xrange(1, (locatorCount + 1)):
            newTurret = turret.Turret_sc2()
            eveTurretPair = newTurret.Initialize(type, (('locator_turret_' + str(x)) + 'a'), (('locator_turret_' + str(x)) + 'b'))
            ship.turrets.append(eveTurretPair)
            self.turrets.append(newTurret)





class EntityShip(spaceObject.SpaceObject):
    __guid__ = 'spaceObject.EntityShip'

    def __init__(self):
        spaceObject.SpaceObject.__init__(self)
        self.gfxTurretID = None
        self.boosters = []
        self.targets = []
        self.turretList = []
        self.fitted = False
        self.typeID = None
        self.modules = {}
        self.model = None



    def LoadModel(self, fileName = None, useInstance = False):
        slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
        self.typeID = slimItem.typeID
        godmaStateManager = sm.StartService('godma').GetStateManager()
        godmaType = godmaStateManager.GetType(self.typeID)
        self.gfxBoosterID = godmaType.gfxBoosterID
        self.gfxTurretID = godmaType.gfxTurretID
        fileName = self.GetTrinityVersionFilename(cfg.invtypes.Get(slimItem.typeID).GraphicFile())
        spaceObject.SpaceObject.LoadModel(self, fileName, useInstance)



    def Assemble(self):
        if (self.gfxBoosterID == 0):
            self.LogError(('Entity type %s has invalid gfxBoosterID ( not set) ' % self.typeID))
            self.gfxBoosterID = 395
        if ((self.model is not None) and (self.model.__bluetype__ == 'trinity.EveShip2')):
            self.AssembleScene2()
            self.SetupAmbientAudio()



    def AssembleScene2(self):
        self.FitBoosters2(self.gfxBoosterID)
        if hasattr(self.model, 'ChainAnimationEx'):
            self.model.ChainAnimationEx('NormalLoop', 0, 0, 1.0)



    def LookAtMe(self):
        if (self.model is None):
            return 
        if not self.fitted:
            self.FitHardpoints()



    def FitHardpoints(self):
        if (self.model is None):
            return 
        if (self.model is None):
            self.LogWarn('FitHardpoints - No model')
            return 
        if self.fitted:
            return 
        self.fitted = True
        if turret.TurretSet.Winter2010TurretsEnabled_ToBeRemoved():
            turretDesignation = util.GraphicFile(self.gfxTurretID)
            if (turretDesignation.strip() == ''):
                return 
            turrets = turret.TurretSet()
            turrets.Initialize(turretDesignation, 1, count=self.model.GetTurretLocatorCount())
            self.modules[self.id] = turrets
            for each in turrets.GetTurretSets():
                self.model.turretSets.append(each)

            self.model.RebuildTurretPositions()
            return 
        if (int(self.gfxTurretID) != 0):
            turretDesignation = util.GraphicFile(self.gfxTurretID)
            if (turretDesignation.strip() != ''):
                turrets = TurretCollection_Sc2()
                turrets.Initialize(self.model, turretDesignation)
                self.modules[self.id] = turrets
        self.model.RebuildCachedLocatorIndices()



    def FitBoosters(self, graphicID):
        graphicURL = util.GraphicFile(graphicID)
        if (graphicURL == ''):
            slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
            self.LogError('Entity type', slimItem.typeID, 'has invalid gfxBoosterID (', graphicID, ')')
            self.gfxBoosterID = 395
        b = blue.os.LoadObject(graphicURL)
        self.boosterType = split(graphicURL, '/')[-1:][0][:-5]
        for booster in self.boosters:
            del booster.children[:]
            booster.object = b.object.CopyTo()
            booster.display = 1




    def Explode(self):
        if (self.typeID is None):
            return 
        else:
            effectOffset = (self.typeID % 3)
            if (self.radius < 100.0):
                explosionURL = entityExplosionsS[(self.typeID % 3)]
            elif (self.radius < 400.0):
                explosionURL = entityExplosionsM[(self.typeID % 3)]
            else:
                explosionURL = entityExplosionsL[(self.typeID % 3)]
            return spaceObject.SpaceObject.Explode(self, explosionURL, randomRotation=True)



    def Release(self):
        if self.released:
            return 
        for loc in self.turretList:
            if hasattr(loc, 'object'):
                loc.object = None

        for turretPair in self.modules.itervalues():
            turretPair.Release()
            turretPair.owner = None

        self.modules = {}
        self.boosters = []
        self.targets = []
        self.turretList = []
        spaceObject.SpaceObject.Release(self)



exports = {'spaceObject.EntityShip': EntityShip}

